<?php
class UsersController extends AppController {

	var $name = 'Users';

	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function home(){
		//$this->Layout->$title_for_layout = "Estudantes";
		$this->pageTitle = "Home";
		$this->loadModel("Opportunity");
		$this->set("Opportunities", $this->Opportunity->find("all", array('limit'=>10)));
	}
	
	/**
	 * Função para exibir dados do usuário
	 * Exclusivo do Administrador
	 * Enter description here ...
	 * @param int $id - Id do usuário cadastrado no BD
	 */
	function view($id = null) {
		if($this->Session->Read('user.email') == '')
			$this->redirect(array('action' => 'home'));
		if($this->Session->Read('user.level') != '1')
			$this->redirect(array('action' => 'home'));
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	
	}

	function add() {
		if (!empty($this->data)) {
			if(trim($this->data['User']['name']) == '')
				$this->Session->setFlash(__('The user NAME can not not empty', true));
			else if (trim($this->data['User']['email']) == '')
				$this->Session->setFlash(__('The user EMAIL can not not empty', true));
			else if (trim($this->data['User']['password']) == '')
				$this->Session->setFlash(__('The user PASSWORD can not not empty', true));
			else if(trim($this->data['User']['email']) != trim($this->data['User']['email2']))
				$this->Session->setFlash(__('The user EMAIL does not match', true));
			else if(trim($this->data['User']['password']) != trim($this->data['User']['password2']))
				$this->Session->setFlash(__('The user PASSWORD does not match', true));
			else{
				// Na tentativa de setarem o tipo do usuario sem ser o adm
				if($this->Session->Read('user.level') != 1 && !empty($this->data['User']['tipo'])){
					$this->Session->setFlash(__('Intern Problem, please try again', true));
				} else {
					$this->User->create();
					$this->data['User']['password'] = md5($this->data['User']['password']);
					if ($this->User->save($this->data)) {
						$this->Session->setFlash(__('The user has been saved', true));
						$this->redirect(array('action' => 'home'));
					} else {
						$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
					}
				}
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid student', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->User->id = $id;
			$this->data = $this->User->read();
			//pr($this->data);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Id inválido', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function login(){
		if($this->Session->Read('user.id') == ''){
			if(!empty($this->data)){
				if(empty($this->data['User']['email']) || empty($this->data['User']['password'])){
					$this->Session->setFlash('Please enter all inputs!');
				} else {
					$pwd = md5($this->data['User']['password']);
					$user = $this->User->find('all', array('conditions' => array('email' => $this->data['User']['email'], 'password' => $pwd)));
					if(count($user)){
						$lvl = $user[0]['User']['level'];
						$this->Session->write('user.id', $user[0]['User']['id']);
						$this->Session->write('user.email', $user[0]['User']['email']);
						$this->Session->write('user.name', $user[0]['User']['name']);
						$this->Session->write('user.level', $user[0]['User']['level']);
						$this->redirect(array('action' => 'home'));
					} else {
						$this->Session->setFlash(__("Email or Password doen't exists!", true));
					}
				}
			}
		} else {
			$this->Session->setFlash("Você já está logado, por favor efetue logout para entrar com outros dados");
			$this->redirect(array('action' => 'home'));
		}
	}
	
	function logout(){
		$this->Session->delete('user');
		$this->redirect(array('action' => 'home'));
	}
	
}
?>